package cn.edu.nuaa.computer.controller;

import cn.edu.nuaa.computer.utils.ExpressionUtils;
import cn.edu.nuaa.computer.utils.TreeStack;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.text.Font;

/**
 * 采用l表示ln，e表示自然常数，p表示圆周率，s表示sin，c表示cos，t表示tan，根号使用g表示，m表示次幂
 */
public class HelloController {
    private StringBuffer representation = new StringBuffer();
    private StringBuffer representationShow = new StringBuffer();

    public Label extLabel;
    // 表达式
    @FXML
    public Label resultLabel;

    @FXML
    public void onResultButton(ActionEvent actionEvent) {
        TreeStack treeFromString;
        try {
            treeFromString = ExpressionUtils.getTreeFromString(ExpressionUtils.replacePiAndE(representation.toString()));
            Double bearFromTree = ExpressionUtils.getBearFromTree(treeFromString);
            resultLabel.setText(String.format("%.2f", bearFromTree));
        } catch (Exception e) {
            e.printStackTrace();
            resultLabel.setText("运算表达式不合法");
        }

    }

    @FXML
    public void onZeroButton(ActionEvent actionEvent) {
        representation.append(0);
        representationShow.append(0);
        if (representationShow.toString().length() >= 48) {
            extLabel.setFont(new Font(18));
        }else {
            extLabel.setFont(new Font(24));
        }
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onOneButton(ActionEvent actionEvent) {
        representation.append(1);
        representationShow.append(1);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onTwoButton(ActionEvent actionEvent) {
        representation.append(2);
        representationShow.append(2);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onThreeButton(ActionEvent actionEvent) {
        representation.append(3);
        representationShow.append(3);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onFourButton(ActionEvent actionEvent) {
        representation.append(4);
        representationShow.append(4);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onFiveButton(ActionEvent actionEvent) {
        representation.append(5);
        representationShow.append(5);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onSixButton(ActionEvent actionEvent) {
        representation.append(6);
        representationShow.append(6);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onSevenButton(ActionEvent actionEvent) {
        representation.append(7);
        representationShow.append(7);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onEightButton(ActionEvent actionEvent) {
        representation.append(8);
        representationShow.append(8);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onNineButton(ActionEvent actionEvent) {
        representation.append(9);
        representationShow.append(9);
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onEmptyButton(ActionEvent actionEvent) {
        representation = new StringBuffer();
        representationShow = new StringBuffer();
        setSize();
        extLabel.setText("0");
    }

    @FXML
    public void onSpotLabel(ActionEvent actionEvent) {
        representation.append(".");
        representationShow.append(".");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 除
    @FXML
    public void onDooButton(ActionEvent actionEvent) {
        representation.append("/");
        representationShow.append("÷");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 乘
    @FXML
    public void onTraButton(ActionEvent actionEvent) {
        representation.append("*");
        representationShow.append("×");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 减
    @FXML
    public void onSubButton(ActionEvent actionEvent) {
        representation.append("-");
        representationShow.append("-");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 加
    @FXML
    public void onAddButton(ActionEvent actionEvent) {
        representation.append("+");
        representationShow.append("+");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 删除
    @FXML
    public void onDelButton(ActionEvent actionEvent) {
        if (representation.toString().length() > 0) {
            if (representation.charAt(representation.length() - 1) == 'l') {
                representationShow.deleteCharAt(representationShow.length() - 1);
            }
            if (representation.charAt(representation.length() - 1) == 's' || representation.charAt(representation.length() - 1) == 'c' || representation.charAt(representation.length() - 1) == 't') {
                representationShow.deleteCharAt(representationShow.length() - 1);
                representationShow.deleteCharAt(representationShow.length() - 1);
            }
            representation.deleteCharAt(representation.length() - 1);
            representationShow.deleteCharAt(representationShow.length() - 1);
            setSize();
            extLabel.setText(representationShow.toString());
        }
    }

    @FXML
    public void onLeftButton(ActionEvent actionEvent) {
        representation.append("(");
        representationShow.append("(");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    @FXML
    public void onRightButton(ActionEvent actionEvent) {
        representation.append(")");
        representationShow.append(")");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 开根
    public void onSqrtButton(ActionEvent actionEvent) {
        representation.append("g");
        representationShow.append("√");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 幂次
    public void onOrderButton(ActionEvent actionEvent) {
        representation.append("^");
        representationShow.append("^");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 自然常数e
    public void onEButton(ActionEvent actionEvent) {
        representation.append("e");
        representationShow.append("e");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 自然对数ln
    public void onLnButton(ActionEvent actionEvent) {
        representation.append("l");
        representationShow.append("ln");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 正弦函数sin
    public void onSinButton(ActionEvent actionEvent) {
        representation.append("s");
        representationShow.append("sin");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 余弦函数
    public void onCosButton(ActionEvent actionEvent) {
        representation.append("c");
        representationShow.append("cos");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 正切函数
    public void onTanButton(ActionEvent actionEvent) {
        representation.append("t");
        representationShow.append("tan");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 圆周率pi
    public void onPiButton(ActionEvent actionEvent) {
        representation.append("p");
        representationShow.append("π");
        setSize();
        extLabel.setText(representationShow.toString());
    }

    // 设置字体大小
    private void setSize(){
        if (representationShow.toString().length() >= 48) {
            extLabel.setFont(new Font(18));
        }else {
            extLabel.setFont(new Font(24));
        }
    }

    public void onTestButton(ActionEvent actionEvent) {
        representation.append("^(0-1)");
        onResultButton(actionEvent);
    }
}
